// pages/mypage/mypage.js
var Mcaptcha = require('../../utils/mcaptcha.js');
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teacherId: undefined,
    password: undefined,
    secretKey:undefined,
    bindCode: undefined,
    name:"",
    codeFlag: -2
  },

  getBindCode: function (e) {
    this.setData({
      bindCode: e.detail.value
    })
    this.checkBindCode();
  },
  checkBindCode: function () {
    var that = this
    var iscode = this.mcaptcha.validate(this.data.bindCode);
    if (this.data.bindCode == undefined) {
      that.setData({
        codeFlag: 0
      })
    } else if (iscode) {
      that.setData({
        codeFlag: 1
      })
    } else {
      that.setData({
        codeFlag: -1
      })
    }
    console.log(this.data.codeFlag);
  },
  changeBind() {
    this.mcaptcha.refresh();
  },

  goRegist: function (e) {
    wx.redirectTo({
      url: '/pages/teacherRegistPage/teacherRegistPage',
    })
  },

  getTeacherId: function (e) {
    console.log(e.detail.value)
    this.setData({
      teacherId: e.detail.value
    })
  },

  getPassword: function (e) {
    console.log(e.detail.value)
    this.setData({
      password: e.detail.value
    })
  },
  getSecretKey: function (e) {
    console.log(e.detail.value)
    this.setData({
      secretKey: e.detail.value
    })
  },


  login: function () {
   
   
      if (this.data.password&&this.data.teacherId&&this.data.secretKey)
      { 
        if (this.data.bindCode == undefined) {
          wx.showModal({
            title: '温馨提示',
            content: '请输入验证码',
            showCancel: false,
            success: function (res) {

            }
          })
        }
        else if (this.data.codeFlag==-1) {
          wx.showModal({
            title: '温馨提示',
            content: '请输入正确验证码',
            showCancel: false,
            success: function (res) {

            }
          })
          this.changeBind();
        }
        else if(this.data.codeFlag==1){

          var that=this
        wx.request({  //这一步发起网络请求 
          
          url: 'https://weixin0304.cn/teacherLogin.php',


          data: {
            teacherId: this.data.teacherId,
            password: this.data.password,
            secretKey:this.data.secretKey
          },
          header: { //'Content-Type': 'application/json'
            'content-type': 'application/json;charset=utf-8'
          },
          method: 'GET',

          success: function (res) {
            console.log(res);
            console.log(res.data);
            //result=1表示登录成功
            if (res.data[0] == 1) {
              //存储用户名
              /*app.globalData.name = res.data[1][3];
              app.globalData.id = res.data[1][0];*/
              var name2 = res.data[1][3]
              wx.setStorageSync('id', that.data.teacherId);
              wx.setStorageSync('name', res.data[1][3]);
              wx.setStorageSync('role', "老师");
              wx.redirectTo({
                url: "/pages/teacherAcPage/teacherAcPage?id=" + that.data.teacherId + "&name=" + name2,
              })
              wx.set
            } else if(res.data==0) {
              wx.showModal({
                title: '温馨提示',
                content: '用户名密码错误',
                showCancel: false,
                success: function (res) {

                }
              })
            }
            else{
              wx.showModal({
                title: '温馨提示',
                content: '请输入正确密钥呦',
                showCancel: false,
                success: function (res) {

                }
              })
            }
            console.log('submit success');
          },
          fail: function (res) {
            wx.showToast({
              title: '您的网络开小差啦~~~',
              icon: "none"
            })
          },
          complete: function (res) {
            console.log('submit complete');
          }
        })
        }
      } else {
        wx.showToast({
          title: '请将信息填写完整后提交',
          icon: "none"
        })
      }
      
    },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mcaptcha = new Mcaptcha({
      el: 'canvas',
      width: 80,
      height: 35,
      createCodeImg: ""
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})